package com.holystone.orca.sys.shiro;

import java.util.HashSet;
import java.util.Set;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class MyRealm extends AuthorizingRealm{  
    /* 
     * @Autowired UserService userService; 
     *  
     * @Autowired RoleService roleService; 
     *  
     * @Autowired LoginLogService loginLogService; 
     */  
  
    public MyRealm() {  
        super();  
  
    }  
  
    @Override  
    protected AuthorizationInfo doGetAuthorizationInfo(  
            PrincipalCollection principals) {  
        /* 这里编写授权代码 */  
    	if (principals == null) {
            throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
        }
        Set<String> roleNames = new HashSet<String>();  
        Set<String> permissions = new HashSet<String>();  
        roleNames.add("admin");  
        permissions.add("admin");  
        System.out.println("doGetAuthorizationInfo方法");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames);  
        info.setStringPermissions(permissions);  
        info.addRoles(roleNames);
        return info;  
    }  
    @Override  
    protected AuthenticationInfo doGetAuthenticationInfo(  
            AuthenticationToken authcToken) throws AuthenticationException {  
        /* 这里编写认证代码 */  
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;  
//      User user = securityApplication.findby(upToken.getUsername());  
//        User user = new User();  
//        user.setUsercode(token.getUsername());  
//        user.setUserName("admin");  
//        user.setPassword(EncryptUtils.encryptMD5("admin"));  
//      if (user != null) {  
        System.out.println("doGetAuthenticationInfo方法");
        System.out.println("方法");
        return new SimpleAuthenticationInfo(token.getUsername(),  
                "1", getName());  
    }  
}  